// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

var UmengNative = require("UmengNative")
var UnitTools = require("UnitTools");
var Config = require("Config");
var PlatForm = require("PlatForm")
var TestLogin = require("TestLogin");
var LoginManager = require("LoginManager")
var HotUpdate = require("HotUpdate");
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        xieyiCheck: cc.Toggle,//协议按钮
        weixinLoginBn: cc.Node,
        testLoginCom:TestLogin//测试登录UI
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self = this;
        var normalStart = function () {
            if(!PlatForm.isAnroid() || !PlatForm.isIOS()){
                this.testLoginCom.node.active = true;
                this.testLoginCom.setLoginEvent(function (account) {
                    cc.log("登录按钮响应");
                    LoginManager.testLogin(account);
                })
            }
            this.weixinLoginBn.on(cc.Node.EventType.TOUCH_START, function (args) {
                if (!this.xieyiCheck.isChecked)return;
                UmengNative.weixinLogin(function (loginData) {
                    cc.log("微信登录结果：")
                    var openid = loginData.openid;
                    var access_token = loginData.access_token;
                    var refresh_token = loginData.refresh_token;

                    UnitTools.request(Config.weixinLoginUrl, {
                            openid: openid,
                            access_token: access_token,
                            refresh_token: refresh_token
                        },
                        function (err,data) {
                            cc.log(data);
                            if(err)return;//提示错误信息
                            LoginManager.loginToHall(data);
                        },5000);
                })
            }, this);
        }.bind(this);
        
        
        
        var hot = new HotUpdate();
        hot.checkUpdate(function (isNew) {
            if(isNew){//有新版本
                var tipUi = cc.find("bg/tip",self.node);
                tipUi.active = true;
                var tipText = cc.find("text",tipUi);
                tipText.getComponent(cc.Label).string = "更新中:0%";
                hot.update(function () {//更新完成
                    cc.director.loadScene("login");
                },function () {//发生错误
                    tipText.getComponent(cc.Label).string = "更新错误，请重新进入游戏";
                },function (process) {
                    tipText.getComponent(cc.Label).string = "更新中:+"+process+"%";
                })
            }else{//没有新版本
                normalStart();
            }
        })
    },

    start () {

    },

    // update (dt) {},
});
